import 'package:stacked/stacked.dart';
import 'package:vista/shared/models/comment_shot.dart';
import 'package:vista/shared/models/moment_shot.dart';
import 'package:vista/shared/models/user_frame.dart';
import 'package:vista/shared/services/auth_focus.dart';
import 'package:vista/shared/services/post_studio.dart';
import 'package:vista/shared/services/relation_bond.dart';

/// 摄影动态详情视图模型
/// 管理摄影动态详情页面的数据和状态
class PhotographyDetailViewModel extends BaseViewModel {
  final AuthFocus _authFocus;
  final PostStudio _postStudio;
  final RelationBond _relationBond;
  final String postId;

  PhotographyDetailViewModel(
    this._authFocus,
    this._postStudio,
    this._relationBond,
    this.postId,
  );

  MomentShot? _post;
  MomentShot? get post => _post;

  UserFrame? _author;
  UserFrame? get author => _author;

  UserFrame? _currentUser;
  UserFrame? get currentUser => _currentUser;

  List<CommentShot> _comments = [];
  List<CommentShot> get comments => _comments;

  bool _isLiked = false;
  bool get isLiked => _isLiked;

  int _likeCount = 0;
  int get likeCount => _likeCount;

  int _commentCount = 0;
  int get commentCount => _commentCount;

  bool _isFollowing = false;
  bool get isFollowing => _isFollowing;

  /// 初始化数据
  Future<void> initialize() async {
    setBusy(true);
    try {
      await _loadPost();
      await _loadAuthor();
      await _loadCurrentUser();
      await _loadLikeStatus();
      await _loadComments();
      await _checkFollowingStatus();
    } finally {
      setBusy(false);
    }
  }

  /// 加载动态
  Future<void> _loadPost() async {
    _post = await _postStudio.getPostById(postId);
    notifyListeners();
  }

  /// 加载发布者信息
  Future<void> _loadAuthor() async {
    if (_post == null) return;
    final users = await _authFocus.getUsers();
    _author = users.firstWhere(
      (u) => u.id == _post!.authorId,
      orElse: () => users.first,
    );
    notifyListeners();
  }

  /// 加载当前用户
  Future<void> _loadCurrentUser() async {
    _currentUser = await _authFocus.getCurrentUser();
    notifyListeners();
  }

  /// 加载点赞状态
  Future<void> _loadLikeStatus() async {
    if (_post == null || _currentUser == null) {
      _likeCount = await _postStudio.getLikeCount(postId);
      return;
    }

    _isLiked = await _postStudio.isLiked(postId, _currentUser!.id);
    _likeCount = await _postStudio.getLikeCount(postId);
    notifyListeners();
  }

  /// 加载评论
  Future<void> _loadComments() async {
    if (_post == null) return;
    _comments = await _postStudio.getComments(_post!.id);
    _commentCount = _comments.length;
    notifyListeners();
  }

  /// 检查关注状态
  Future<void> _checkFollowingStatus() async {
    if (_currentUser == null || _author == null) return;
    if (_currentUser!.id == _author!.id) return;

    _isFollowing = await _relationBond.isFollowing(
      followerId: _currentUser!.id,
      followingId: _author!.id,
    );
    notifyListeners();
  }

  /// 切换点赞
  Future<void> toggleLike() async {
    if (_currentUser == null || _post == null) return;

    if (_isLiked) {
      await _postStudio.unlikePost(_post!.id, _currentUser!.id);
    } else {
      await _postStudio.likePost(_post!.id, _currentUser!.id);
    }

    _isLiked = !_isLiked;
    _likeCount = await _postStudio.getLikeCount(_post!.id);
    notifyListeners();
  }

  /// 添加评论
  Future<void> addComment(String content) async {
    if (_currentUser == null || _post == null) return;

    await _postStudio.addComment(
      postId: _post!.id,
      userId: _currentUser!.id,
      content: content,
    );
    await _loadComments();
  }

  /// 删除评论
  Future<void> deleteComment(String commentId) async {
    if (_post == null) return;
    await _postStudio.deleteComment(commentId, _post!.id);
    await _loadComments();
  }

  /// 获取评论用户
  Future<UserFrame?> getCommentUser(String userId) async {
    final users = await _authFocus.getUsers();
    try {
      return users.firstWhere((u) => u.id == userId);
    } catch (e) {
      return null;
    }
  }

  /// 删除动态
  Future<void> deletePost() async {
    if (_post == null) return;
    await _postStudio.deletePost(_post!.id);
  }

  /// 刷新认证和点赞状态
  Future<void> refreshAuthAndLikeStatus() async {
    await _loadCurrentUser();
    await _loadLikeStatus();
    await _checkFollowingStatus();
  }

  /// 切换关注
  Future<void> toggleFollow() async {
    if (_currentUser == null || _author == null) return;
    if (_currentUser!.id == _author!.id) return;

    if (_isFollowing) {
      await _relationBond.unfollowUser(
        followerId: _currentUser!.id,
        followingId: _author!.id,
      );
    } else {
      await _relationBond.followUser(
        followerId: _currentUser!.id,
        followingId: _author!.id,
      );
    }

    _isFollowing = !_isFollowing;
    notifyListeners();
  }
}
